<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">

    <!-- include jquery mobile -->
    <link rel="stylesheet" href="jquery/jquery.mobile-1.0b2.min.css" />
    <script src="jquery/jquery-1.6.2.min.js"></script>
    <script src="jquery/jquery.mobile-1.0b2.min.js"></script>
        
    <!-- include phonegap / callback -->
	<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
    <!-- required for oauth plugin support -->    
    <script type="text/javascript" src="SalesforceOAuthPlugin.js"></script>

    <!-- include forcetk for REST transaction support -->
    <script src="forcetk.js"></script>
    
    <!-- include local utility functionality -->
    <link rel="stylesheet" type="text/css" href="SFHybridApp.css" />
    <script type="text/javascript" charset="utf-8" src="SFHybridApp.js"></script>

    <script type="text/javascript" charset="utf-8">

    
    // The version of the REST API you wish to use in your app.
    var apiVersion = "v23.0";

	// If you want to prevent dragging, uncomment this section
	/*
	function preventBehavior(e) 
	{ 
      e.preventDefault(); 
    };
	document.addEventListener("touchmove", preventBehavior, false);
	*/
	
	/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
	see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
	for more details -jm */
	/*
	function handleOpenURL(url)
	{
		// do something with the url passed in.
	}
	*/
	
    var forcetkClient;
    var debugMode = true;

    jQuery(document).ready(function() {
        //Add event listeners and so forth here
        SFHybridApp.logToConsole("onLoad: jquery ready");
		document.addEventListener("deviceready", onDeviceReady,false);

    });

    // When this function is called, PhoneGap has been initialized and is ready to roll 
    function onDeviceReady() {
        SFHybridApp.logToConsole("onDeviceReady: PhoneGap ready");
		//Call getAuthCredentials to get the initial session credentials
        SalesforceOAuthPlugin.getAuthCredentials(salesforceSessionRefreshed, getAuthCredentialsError);

        //register to receive notifications when autoRefreshOnForeground refreshes the sfdc session
        document.addEventListener("salesforceSessionRefresh",salesforceSessionRefreshed,false);

        //enable buttons
        regLinkClickHandlers();
    }
        

    function salesforceSessionRefreshed(creds) {
        SFHybridApp.logToConsole("salesforceSessionRefreshed");
		
        forcetkClient = new forcetk.Client(creds.clientId, creds.loginUrl);
        forcetkClient.setSessionToken(creds.accessToken, apiVersion, creds.instanceUrl);
        forcetkClient.setRefreshToken(creds.refreshToken);
        forcetkClient.setUserAgentString(creds.userAgent);
    }


    function getAuthCredentialsError(error) {
        SFHybridApp.logToConsole("getAuthCredentialsError: " + error);
    }
        
    </script>
        
	<!-- load our app-specific code -->
    <script src="inline.js"></script>       

  </head>
  <body>
      <div data-role="page" data-theme="b" id="jqm-home"> 
          <div  data-role="header"> 
              <h1>Hybrid REST Explorer (SalesforceMobileSDK + PhoneGap + JQM)</h1> 
          </div> 

          <div id="#content" data-role="content"> 

              <p><a href="#" id="link_fetch_device_contacts" data-role="button" data-inline="true">Fetch Device contacts</a></p>
              <p><a href="#" id="link_fetch_sfdc_contacts" data-role="button" data-inline="true">Fetch SFDC status</a></p>
              <p><a href="#" id="link_fetch_sfdc_accounts" data-role="button" data-inline="true">Fetch SFDC Accounts</a></p>
              <p><a href="#" id="link_reset" data-role="button" data-inline="true">Reset</a></p>

              <p><a href="#" id="link_logout" data-role="button" data-inline="true">Logout</a></p>

              <div id="div_device_contact_list"></div>
              <div id="div_sfdc_contact_list"></div>
              <div id="div_sfdc_account_list"></div>

              <h3>console:</h3>
              <p id="console" class="logWindow">
              </p>
          </div> 

      </div> 
  </body>
</html>
